package net.htjs.juc.completablefuture;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 异步任务
 */
@Slf4j
public class Demo1 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 无返回值的任务
        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            log.info("异步任务执行");
        });
        future.join();

        // 有返回值的任务
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            log.info("异步任务执行");
            return "hello";
        });
        future2.join();
        // 阻塞获取结果（需处理InterruptedException/ExecutionException）
        log.info("结果是:{}", future2.get());
        log.info("结果是:{}", future2.getNow("默认值"));
    }

}
